package com.aring.deals.actions;

import java.lang.reflect.Type;

import org.apache.struts2.ServletActionContext;

import com.aring.constant.CommConstant;
import com.aring.constant.ResultCode;
import com.aring.deals.bean.JsonBean;
import com.aring.deals.bean.User;
import com.aring.deals.listener.OptionListener;
import com.aring.deals.option.UserOption;
import com.aring.deals.tool.Common;
import com.google.gson.reflect.TypeToken;
import com.opensymphony.xwork2.ActionContext;

@SuppressWarnings("serial")
public class SetWithdrawAccountAction extends BasicAction{
   private String account;
   private String type;

	@Override
	public String execute() throws Exception {
		setOut(ServletActionContext.getResponse().getWriter());
		if(getId()>0){
			UserOption.setWithdrawAccount(getId(),getUserpass(),CommConstant.CLIENT_APP,account, type, listener);
		}else {
			Integer id = (Integer) ActionContext.getContext().getSession().get(CommConstant.SESSION_USER_ID);
		    if(id==null) handleNotLogin();
		    else {
		     setId(id);
			 UserOption.setWithdrawAccount(getId(),getUserpass(),CommConstant.CLIENT_BROWSER,account, type, listener);
		}
		}
		return null;
	}

	
	private OptionListener<User> listener = new OptionListener<User>() {
		
		@Override
		public void onSuccess(User user) {
			JsonBean<String> json = new JsonBean<String>();
			json.setResultCode(ResultCode.SUCCESS_CODE);
            Type type = new TypeToken<JsonBean<String>>() {}.getType();
			getOut().write(Common.<JsonBean<String>>packageJson(json, type));
			getOut().flush();
			getOut().close();
		}
		
		@Override
		public void onFailure(int resultCode) {
			JsonBean<String> json = new JsonBean<String>();
			json.setResultCode(resultCode);
            Type type = new TypeToken<JsonBean<String>>() {}.getType();
			getOut().write(Common.<JsonBean<String>>packageJson(json, type));
			getOut().flush();
			getOut().close();
		}
	};
	

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	
	
	
	
	
	
	
	
	
	
}
